package com.axzhengxin.platform.afterloan.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.axzhengxin.platform.afterloan.domain.InventoryLoanInfo;
import com.axzhengxin.platform.afterloan.service.IInventoryLoanInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * InventoryLoanInfoController
 * 存量业务访问端口
 *
 * @author jiangnan
 * @date 2021/4/1
 */
@RestController
@RequestMapping("/afterloan/inventory-loan")
public class InventoryLoanInfoController extends BaseController {

    @Autowired
    private IInventoryLoanInfoService inventoryLoanInfoService;

    /**
     * 查询存量业务列表
     */
    /*@PreAuthorize("@ss.hasPermi('afterloan:inventory:list')")*/
    @GetMapping("/list")
    public TableDataInfo list(InventoryLoanInfo inventoryLoanInfo) {
        startPage();
        List<InventoryLoanInfo> list = inventoryLoanInfoService.selectInventoryLoanInfoList(inventoryLoanInfo);
        return getDataTable(list);
    }

    /**
     * 导出存量业务列表
     */
    /*@PreAuthorize("@ss.hasPermi('afterloan:inventory:export')")*/
    @Log(title = "存量业务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InventoryLoanInfo inventoryLoanInfo) {

        List<InventoryLoanInfo> list = inventoryLoanInfoService.selectInventoryLoanInfoList(inventoryLoanInfo);
        ExcelUtil<InventoryLoanInfo> util = new ExcelUtil<>(InventoryLoanInfo.class);
        return util.exportExcel(list, "upload");
    }

    /**
     * 获取存量业务详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('afterloan:inventory:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(inventoryLoanInfoService.selectInventoryLoanInfoById(id));
    }

    /**
     * 新增存量业务
     */
    /*@PreAuthorize("@ss.hasPermi('afterloan:inventory:add')")*/
    @Log(title = "存量业务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody InventoryLoanInfo inventoryLoanInfo) {
        return toAjax(inventoryLoanInfoService.insertInventoryLoanInfo(inventoryLoanInfo));
    }

    /**
     * 修改存量业务
     */
    /*@PreAuthorize("@ss.hasPermi('afterloan:inventory:edit')")*/
    @Log(title = "存量业务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody InventoryLoanInfo inventoryLoanInfo) {
        return toAjax(inventoryLoanInfoService.updateInventoryLoanInfo(inventoryLoanInfo));
    }

    /**
     * 删除存量业务
     */
    /*@PreAuthorize("@ss.hasPermi('afterloan:inventory:remove')")*/
    @Log(title = "存量业务", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(inventoryLoanInfoService.deleteInventoryLoanInfoByIds(ids));
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<InventoryLoanInfo> util = new ExcelUtil<>(InventoryLoanInfo.class);
        return util.importTemplateExcel("存量业务数据");
    }

    @Log(title = "存量业务", businessType = BusinessType.IMPORT)
   /* @PreAuthorize("@ss.hasPermi('afterloan:inventory:import')")*/
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<InventoryLoanInfo> util = new ExcelUtil<>(InventoryLoanInfo.class);
        List<InventoryLoanInfo> loanInfoList = util.importExcel(file.getInputStream());
        InventoryLoanInfoImportResult result = inventoryLoanInfoService.importInventoryLoanInfo(loanInfoList, updateSupport);
        String msg="";
        if (result.failure()) {
            for (String s : result.getFailureMessageList()) {
                msg+="<br>"+s+";</br>";
            }
            return AjaxResult.error(msg);
        } else {
            return AjaxResult.success(StrUtil.format("已成功导入存量业务数据{}条", result.getSuccessCount()));
        }
    }

    @Data
    public static class InventoryLoanInfoImportResult {

        /**
         * 失败消息集合
         */
        private List<String> failureMessageList = new ArrayList<>();

        /**
         * 成功消息集合
         */
        private Integer successCount = 0;

        /**
         * 是否包含失败消息
         *
         * @return 判断结果
         */
        public boolean failure() {
            return CollUtil.isNotEmpty(failureMessageList);
        }

    }

}
